$("#cancel").click(function(){
    window.location = "<?php echo $this->url(array('module' => 'admin', 'controller' => 'destination', 'action' => 'index'), null, true); ?>";
});

$("#image").change(function(){
    $("#has-image").val("1");
});

var arr_startDate = new Array();
var arr_surcharge = new Array();

function removeArr(i){
    arr_startDate[i] = '';
}

function removeArrSurcharge(i){
    arr_surcharge[i] = '';
}

function removedateS(id){
    $("#" + id).remove();
}

function createDateSchedule(length){
    for(var i = length; i > 0; i --){
        var html = '<div id="date_' + i + '" class="form-group <?php echo $this->classError['destination']; ?>">';
        html += '<label class="col-lg-3 control-label"><?php echo $this->translate('date'); ?> ' + i + '</label>';
        html += '<div class="col-lg-7">';
        html += '<input type="text" value="<?php echo $this->data['destination']; ?>" name="date[' + i + '][destination]" class="form-control" placeholder="<?php echo $this->translate('destination'); ?>" /><br />';
        html += '<input id="image" type="file" name="date[' + i + '][image]" /><br />';
        html += '<textarea rows="20" id="editor_' + i + '" name="date[' + i + '][description]"><?php echo $this->data['description']; ?></textarea>';
        html += '<?php echo $this->messageError['destination']; ?>';
        html += '</div>';
        html += '</div>';
        html += ' ';
        
        $("#tour_schedule").after(html);
        
        tinymce.init({
            selector: "textarea#editor_" + i,
            theme: "modern",
            plugins: [
                "advlist autolink lists link image charmap print preview anchor",
                "searchreplace visualblocks code fullscreen",
                "insertdatetime media table contextmenu paste responsivefilemanager textcolor"
            ],
            toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image | forecolor backcolor",
            external_filemanager_path:"<?php echo $this->templateURL; ?>/js/tiny_mce/filemanager/",
        	filemanager_title:"Responsive Filemanager" ,
        	external_plugins: { "filemanager" : "<?php echo $this->templateURL; ?>/js/tiny_mce/filemanager/plugin.js"}
        });
    }
}

function createSchedule(length){
    if(length > 0){
        createDateSchedule(length);
        $("#alert").remove();
    }else{
        $("div[id^='date_']").remove();
        
        var html = '<div id="alert" class="alert alert-danger">';
        html += '<strong><?php echo $this->translate('empty length'); ?></strong>';
        html += '</div>';
        
        $("#tour_schedule").after(html);
    }
}

$(function() {
    //$("#hotel option[value='1']").attr('selected', 'selected');
    
    $(".stats-tbl-theme").tablecloth({theme: "stats"});
    
    var lengthDay = $("#length").val();
    createSchedule(lengthDay);
    
    $("#length").change(function(){
        lengthDay = $("#length").val();
        createSchedule(lengthDay);
    });
    
    $('#dp').datepicker({
        format: 'dd-mm-yyyy',
        viewMode: 0,
    });
    
    $('#tags_start_date').tagsInput({width: 'auto'});
    
    var i = 0;
    
    $('#addDate').click(function(){
        var date = $("#dp").val();
        $("#dp").val('');
        
        if(date.length > 0){
            if($.inArray(date, arr_startDate) < 0){
                var html = '<div class="alert alert-block alert-success fade in col-lg-3">';
                html += '<button onclick="removeArr(' + i + ')" class="close" type="button" data-dismiss="alert" aria-hidden="true">×</button>';
                html += 'Ngày: <strong>' + date + '</strong>';
                html += '<input type="hidden" name="start_date[]" value="' + date + '" />';
                html += '</div>';
                
                $("#alt_schedule").append(html);
                
                arr_startDate[i] = date;
                i++;
            }
        }else{
            alert('Vui lòng chọn ngày!');
            $('#dp').focus();
        }
    });
    
    var y = 0;
    
    $("#addSurcharge").click(function(){
        var surcharge_id = $("#surcharge").val();
        var surcharg_text = $("#surcharge option:selected").text();
        var surcharge_price_mask = $("#surcharge_price").val();
        var surcharge_price = $("#surcharge_price").unmask();
        
        var f= false;
        var message = '';
        
        if(surcharge_id > 0){
            if(surcharge_price > 0){
                if($.inArray(surcharge_id, arr_surcharge) < 0){
                    var html = '<div class="alert alert-block alert-success fade in col-lg-4">';
                    html += '<button onclick="removeArrSurcharge(' + y + ')" class="close" type="button" data-dismiss="alert" aria-hidden="true">×</button>';
                    html += surcharg_text + ': <strong>' + surcharge_price_mask + '</strong>';
                    html += '<input type="hidden" name="surcharge_price[]" value="' + surcharge_id + '_' + surcharge_price + '" />';
                    html += '</div>';
                    
                    $("#surcharge_list").append(html);
                    
                    arr_surcharge[y] = surcharge_id;
                    y++;
                }
            }else{
                message = 'Giá tiền phải > 0!';
                f = true;
            }
        }else{
            f = true;
            message = 'Vui lòng chọn phí phụ thu!';
        }
        
        if(f){
            alert(message);
            $('#surcharge_price').focus();
        }
    });
    
    $("#tour_type").select2();
    $("#destination").select2();
    $("#hotel").select2();
    $("#guide").select2();
    $("#start_date").select2();
    $("#vehicle").select2();
    $("#surcharge").select2();
    
    $("#surcharge_price").priceFormat({
        prefix: '',
        clearPrefix: true,
        thousandsSeparator: '.',
        centsLimit: 0
    });
    
    $('#stepy_form').stepy({
        backLabel: '<?php echo $this->translate('back'); ?>',
        nextLabel: '<?php echo $this->translate('next'); ?>',
        errorImage: true,
        block: true,
        description: true,
        legend: false,
        titleClick: true,
        titleTarget: '#top_tabby',
        validate: false
    });
    
    tinymce.init({
            selector: "#description",
            theme: "modern",
            plugins: [
                "advlist autolink lists link image charmap print preview anchor",
                "searchreplace visualblocks code fullscreen",
                "insertdatetime media table contextmenu paste responsivefilemanager textcolor"
            ],
            toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image | forecolor backcolor",
            external_filemanager_path:"<?php echo $this->templateURL; ?>/js/tiny_mce/filemanager/",
        	filemanager_title:"Responsive Filemanager" ,
        	external_plugins: { "filemanager" : "<?php echo $this->templateURL; ?>/js/tiny_mce/filemanager/plugin.js"}
        });
});